package com.giveu.signature.common.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Transformer;

public class ListUtils<T> {


	public List<T> getDifferenceList(List<T> allList, List<T> aList) {
		if (aList != null && !aList.isEmpty()) {
			Map<T, T> dataMap = new HashMap<>();
			for (T id : aList) {
				dataMap.put(id, id);
			}
			List<T> newList = new ArrayList<>();
			for (T id : allList) {
				if (!dataMap.containsKey(id)) {
					newList.add(id);
				}
			}
			return newList;
		} else {
			return allList;
		}
	}

	public List<List<T>> createLists(List<T> lists, int size){
		List<List<T>> listArr = new ArrayList<>();
		int arrSize = lists.size() % size == 0 ? lists.size() / size : lists.size() / size + 1;
		for (int i = 0; i < arrSize; i++)
		{
			List<T> sub = new ArrayList<>();
			for (int j = i * size; j <= size * (i + 1) - 1; j++) {
				if (j <= lists.size() - 1) {
					sub.add(lists.get(j));
				}
			}
			listArr.add(sub);
		}
		return listArr;
	}

	public static List<List<String>> createList(List<String> lists, int size){
		List<List<String>> listArr = new ArrayList<>();
		int arrSize = lists.size() % size == 0 ? lists.size() / size : lists.size() / size + 1;
		for (int i = 0; i < arrSize; i++)
		{
			List<String> sub = new ArrayList<>();
			for (int j = i * size; j <= size * (i + 1) - 1; j++) {
				if (j <= lists.size() - 1) {
					sub.add(lists.get(j));
				}
			}
			listArr.add(sub);
		}
		return listArr;
	}

	public static List<List<Long>> createListLong(List<Long> lists, int size){
		List<List<Long>> listArr = new ArrayList<>();
		int arrSize = lists.size() % size == 0 ? lists.size() / size : lists.size() / size + 1;
		for (int i = 0; i < arrSize; i++)
		{
			List<Long> sub = new ArrayList();
			for (int j = i * size; j <= size * (i + 1) - 1; j++) {
				if (j <= lists.size() - 1) {
					sub.add(lists.get(j));
				}
			}
			listArr.add(sub);
		}
		return listArr;
	}

	public static List<String[]> createList(String[] arrays, int size){
		List<String[]> listArr = new ArrayList<>();
		int arrSize = arrays.length % size == 0 ? arrays.length / size : arrays.length / size + 1;
		for (int i = 0; i < arrSize; i++)
		{
			String[] sub = new String[arrays.length % size == 0?size:(i!=arrSize-1?size:arrays.length % size)];
			int h = 0;
			for (int j = i * size; j <= size * (i + 1) - 1; j++) {
				if (j <= arrays.length - 1) {
					sub[h++]=arrays[j];
				}
			}
			listArr.add(sub);
		}
		return listArr;
	}

	public static List<String[]> createList(String[] arrays){
		return createList(arrays,1000);
	}

	public static List<String> collLongToString(List<Long> list) {
		List<String> iList =new ArrayList<String>(list.size());
		CollectionUtils.collect(list, 
				new Transformer() {
					public Object transform(Object input) {
						return String.valueOf(input);
					}
				},iList);
		return iList;
	}
	
	public static <E> List<E> removeDuplicate(List<E> list) {   
	    HashSet<E> h = new HashSet<>(list);   
	    list.clear();   
	    list.addAll(h);   
	    return list;   
	}   
}
